package com.zch.market.controller;

import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zch.market.entity.User;
import com.zch.market.service.UserService;

@Controller
@RequestMapping("UserController")
public class UserController{
	@Autowired
	private UserService us;
	@RequestMapping("getAllUser")
	public String getAllUser(String userName,String pageIndex,
			HttpServletRequest request, HttpServletResponse response){
		//获取模糊查询的userName
		if(pageIndex==null){
			pageIndex = "1";
		}
		System.out.println(userName);
		int pageIndex2 = Integer.parseInt(pageIndex);
		List<User> list = us.getUserByPage(userName, pageIndex2);
		//存放各种数据
		request.setAttribute("userName", userName);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("i",(pageIndex2-1)*5);
		//获得总页数
		int totalPage = us.getTotalPage(userName);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("totalCount", us.getTotalCount(userName));
		request.setAttribute("list", list);
		return "userAdmin";
	}

	@RequestMapping("updateUser")
	public void updateUser(User user,HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		boolean flag = us.updateUser(user);
		PrintWriter out = response.getWriter();
		if(flag){
			//更新成功
			String a = URLEncoder.encode("更新成功！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllUser';</script>");
		}else{
			//更新失败
			String a = URLEncoder.encode("更新失败！", "UTF-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllUser';</script>");
		}
		
	}

	@RequestMapping("deleteUser")
	public void deleteUser(int id,HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		boolean flag = us.deleteUser(id);
		PrintWriter out = response.getWriter();
		if(flag){
			//删除成功
			//解决弹框乱码问题
			String a = URLEncoder.encode("删除成功！", "utf-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllUser';</script>");
		}else{
			//删除失败
			//解决弹框乱码问题
			String a = URLEncoder.encode("删除失败！", "utf-8");
			out.println("<script>alert(decodeURIComponent('"+a+"'));window.location.href='getAllUser';</script>");
		}	
	}

	@RequestMapping("addUser")
	public void addUser(User user,HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		boolean flag = us.addUser(user);
		PrintWriter out = response.getWriter();
		if(flag){
			//添加成功
			//解决弹框乱码问题
			String a = URLEncoder.encode("添加成功！", "utf-8");
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='getAllUser';</script>");
		}else{
			//添加失败
			//解决弹框乱码问题
			String a = URLEncoder.encode("添加失败！", "utf-8");
			out.println("<script>alert(decodeURIComponent('"+a+"'));window.location.href='getAllUser';</script>");
		}	
	}
	
	@RequestMapping("getUserById")
	public String getUserById(int id, HttpServletRequest request){
		User user = us.getUserById(id);
		request.setAttribute("user", user);
		return "userUpdate";
	}

}
